package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.PMF;
import vrp.SaleOrder;

public class SaleOrderDeleteServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {

    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	String responseRedirect = "/saleorders.jsp";
    	
        try {
        	long saleOrderID = Integer.parseInt(req.getParameter("saleOrder"));
        	long partnerID = Integer.parseInt(req.getParameter("client"));

            Key saleOrderKey = KeyFactory.createKey(SaleOrder.class.getSimpleName(), saleOrderID);
            SaleOrder saleOrder = pm.getObjectById(SaleOrder.class, saleOrderKey);

            pm.deletePersistent(saleOrder);
            responseRedirect += "?client=" + partnerID;
        } finally {
            pm.close();
        }
        resp.sendRedirect(responseRedirect);
    }
}